/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.itangqi.waveloadingview.utils;

import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * 日志打印类
 *
 * @since 2021-04-09
 */
public class Logcat {
    private static final boolean LOGD = true;
    private static final boolean LOGI = true;
    private static final boolean LOGW = true;
    private static final boolean LOGE = true;
    private static final boolean LOGF = true;
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x01, "ChinaSoftinc");

    private Logcat() {

    }
    /**
     * 调试
     *
     * @param mess
     * @param args
     */
    public static void d(String mess, Object... args) {
        if (LOGD) {
            HiLog.debug(LABEL, getMsgFormat(mess), args);
        }
    }

    /**
     * 调试
     *
     * @param tag
     * @param mess
     * @param args
     */
    public static void d(String tag, String mess, Object... args) {
        if (LOGD) {
            HiLog.debug(LABEL, tag + ", " + getMsgFormat(mess), args);
        }
    }

    /**
     * 信息
     *
     * @param mess
     * @param args
     */
    public static void i(String mess, Object... args) {
        if (LOGI) {
            HiLog.info(LABEL, getMsgFormat(mess), args);
        }
    }

    /**
     * 信息
     *
     * @param tag
     * @param mess
     * @param args
     */
    public static void i(String tag, String mess, Object... args) {
        if (LOGI) {
            HiLog.info(LABEL, tag + ", " + getMsgFormat(mess), args);
        }
    }

    /**
     * 警告
     *
     * @param mess
     * @param args
     */
    public static void w(String mess, Object... args) {
        if (LOGW) {
            HiLog.warn(LABEL, getMsgFormat(mess), args);
        }
    }

    /**
     * 警告
     *
     * @param tag
     * @param mess
     * @param args
     */
    public static void w(String tag, String mess, Object... args) {
        if (LOGW) {
            HiLog.warn(LABEL, tag + ", " + getMsgFormat(mess), args);
        }
    }

    /**
     * 错误
     *
     * @param mess
     * @param args
     */
    public static void e(String mess, Object... args) {
        if (LOGE) {
            HiLog.error(LABEL, getMsgFormat(mess), args);
        }
    }

    /**
     * 错误
     *
     * @param tag
     * @param mess
     * @param args
     */
    public static void e(String tag, String mess, Object... args) {
        if (LOGE) {
            HiLog.error(LABEL, tag + ", " + getMsgFormat(mess), args);
        }
    }

    /**
     * 致命log
     *
     * @param mess
     * @param args
     */
    public static void f(String mess, Object... args) {
        if (LOGF) {
            HiLog.fatal(LABEL, getMsgFormat(mess), args);
        }
    }

    /**
     * 致命log
     *
     * @param tag
     * @param mess
     * @param args
     */
    public static void f(String tag, String mess, Object... args) {
        if (LOGF) {
            HiLog.fatal(LABEL, tag + ", " + getMsgFormat(mess), args);
        }
    }

    /**
     * 获取相关数据:类名,方法名,行号等.用来定位行
     *
     * @return 返回当前Log所在线程、类，方法、文件、行号信息
     */
    private static String getFunctionName() {
        StackTraceElement[] sts = Thread.currentThread().getStackTrace();
        if (sts != null) {
            for (StackTraceElement st : sts) {
                if (st.isNativeMethod()) {
                    continue;
                }
                if (st.getClassName().equals(Thread.class.getName())) {
                    continue;
                }
                if (st.getClassName().equals(Logcat.class.getName())) {
                    continue;
                }
                return "[ Thread:" + Thread.currentThread().getName() + ", at " + st.getClassName() + "."
                        + st.getMethodName() + "(" + st.getFileName() + ":" + st.getLineNumber() + ")" + " ]";
            }
        }
        return "";
    }

    /**
     * 输出格式定义
     *
     * @param msg
     * @return 返回格式定义
     */
    private static String getMsgFormat(String msg) {
        return msg + " ;" + getFunctionName();
    }
}